﻿<%@ WebHandler Language="C#" Class="SimpleCompute" %>

using System.Collections;
using System.Web;

using monadsphere.js;
using monadsphere.ashx;
using monadsphere.dom.events;
using monadsphere.dom.html2;
using monadsphere.browser;

/// <summary>
/// This class demonstrates a simple application.
/// </summary>
public class SimpleCompute: JSHandler {

    public override void process(HttpContext context, Hashtable data) {

        HTMLDocument doc = HTMLDocument.instance;
        HTMLElement root = doc.body;

        HTMLDivElement div = doc.createElement<HTMLDivElement>();
        div.textContent = "This demostrates local event handling. " +
            "Please fill in two numbers and click Compute button. " +
            "Computation result will be shown through an click event handler " +
            "and an alert will be shown through another click event handler.";
        root.appendChild(div);

        // container
        HTMLDivElement f = doc.createElement<HTMLDivElement>();
        f.style.height = "30px";
        f.style.padding = "5px";

        // input1
        HTMLInputElement input1 = doc.createElement<HTMLInputElement>();
        input1.id = "input1";
        f.appendChild(input1);

        // multiplier x
        HTMLSpanElement multiplier = doc.createElement<HTMLSpanElement>();
        multiplier.textContent = " × ";
        f.appendChild(multiplier);

        // input2
        HTMLInputElement input2 = doc.createElement<HTMLInputElement>();
        input2.id = "input2";
        f.appendChild(input2);

        root.appendChild(f);

        // the button
        HTMLButtonElement button = doc.createElement<HTMLButtonElement>();
        button.textContent = "Compute";
        root.appendChild(button);

        // result area
        HTMLDivElement result = doc.createElement<HTMLDivElement>();
        result.id = "result";
        result.style.padding = "5px";
        root.appendChild(result);

        button.onclick += new HtmlEventHandler<MouseEventListener<HTMLElement>>(compute);
        button.onclick += new HtmlEventHandler<MouseEventListener<HTMLElement>>(alert);
    }

    protected void alert(MouseEvent<HTMLElement> e) {
        HtmlWindow.alert("computation is done");
    }

    protected void compute(MouseEvent<HTMLElement> e) {
        HTMLDocument doc = new HTMLDocument();
        HTMLInputElement input1 = doc.getElementById<HTMLInputElement>("input1");
        HTMLInputElement input2 = doc.getElementById<HTMLInputElement>("input2");
        HTMLDivElement result = doc.getElementById<HTMLDivElement>("result");

        JSNumber number1 = JSNumber.parseFloat(input1.value);
        JSNumber number2 = JSNumber.parseFloat(input2.value);

        result.textContent = number1 * number2;
    }
}
